package com.ejia.ui;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JButton;
import java.awt.Font;
import javax.swing.JTextField;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

import com.ejia.pojo.house;
import com.ejia.pojo.user;
import com.ejia.server.MoneyService;
import com.ejia.server.UserService;

import java.awt.Toolkit;
import java.util.ArrayList;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.SwingConstants;
import javax.swing.ImageIcon;

public class YZXXGL extends JFrame {

	private JPanel contentPane;
	private JTextField txtName;
	private static JTable table;
	private static ArrayList<user> users;
	public static int index;
	
	
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					YZXXGL frame = new YZXXGL();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	
	public static void dataBind(String name) {
		if (name.equals("")) {
			users = UserService.findYZXXGL();
		}else {
			users=UserService.findByYZXXGL(name);
		}
		Object[][] data =new Object[users.size()][];
		for (int i = 0; i < data.length; i++) {
			data[i]=new Object[] {
					users.get(i).getId(),
					users.get(i).getRealname(),
					users.get(i).getHid().getHname(),
					users.get(i).getSex(),
					users.get(i).getUid(),
					users.get(i).getPhone()
			};
		}
		table.setModel(new DefaultTableModel(
				data,
				new String[] {
					"\u7F16\u53F7", "\u59D3\u540D", "房号", "\u6027\u522B", "\u8D26\u53F7", "\u7535\u8BDD"
				}
			));
		table.setRowHeight(40);
		table.setFont(new Font("宋体", Font.PLAIN, 26));
		table.getColumnModel().getColumn(0).setPreferredWidth(30);
		table.getColumnModel().getColumn(1).setPreferredWidth(50);
		table.getColumnModel().getColumn(2).setPreferredWidth(190);
		table.getColumnModel().getColumn(3).setPreferredWidth(15);
		table.getColumnModel().getColumn(5).setPreferredWidth(150);
		table.getTableHeader().setFont(new Font("宋体", Font.PLAIN, 26));
	}
	
	
	/**
	 * Create the frame.
	 */
	public YZXXGL() {
		setIconImage(Toolkit.getDefaultToolkit().getImage(YZXXGL.class.getResource("/com/ejia/photo/客户管理系统.png")));
		setTitle("业主信息管理");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 1054, 689);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JButton button = new JButton("业主缴费");
		button.setIcon(new ImageIcon(YZXXGL.class.getResource("/com/ejia/photo/缴费.png")));
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				new FYGL().setVisible(true);
				dispose();
			}
		});
		button.setFont(new Font("宋体", Font.PLAIN, 24));
		button.setBounds(15, 204, 203, 70);
		contentPane.add(button);
		
		JButton button_1 = new JButton("重置密码");
		button_1.setIcon(new ImageIcon(YZXXGL.class.getResource("/com/ejia/photo/我的-重置密钥.png")));
		button_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
			
			if (table.getSelectedRowCount()>0) {
				int index=table.getSelectedRow();
				int id=users.get(index).getId();
				String input=JOptionPane.showInputDialog("请输入新密码");
				if (input!=null) {
					if (UserService.update(id,input)) {
						JOptionPane.showMessageDialog(null, "修改成功");
					}else {
						JOptionPane.showMessageDialog(null, "修改失败");
					}
				}
			} else {
				JOptionPane.showMessageDialog(null, "请选择用户");
			}
			}
		});
		button_1.setFont(new Font("宋体", Font.PLAIN, 24));
		button_1.setBounds(15, 290, 203, 70);
		contentPane.add(button_1);
		
		JButton button_2 = new JButton("添加信息");
		button_2.setIcon(new ImageIcon(YZXXGL.class.getResource("/com/ejia/photo/添加.png")));
		button_2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				new YZXXGLADD().setVisible(true);
				dispose();
			}
		});
		button_2.setFont(new Font("宋体", Font.PLAIN, 24));
		button_2.setBounds(15, 376, 203, 70);
		contentPane.add(button_2);
		
		JButton button_3 = new JButton("编辑信息");
		button_3.setIcon(new ImageIcon(YZXXGL.class.getResource("/com/ejia/photo/编辑.png")));
		button_3.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				if (table.getSelectedRowCount()>0) {
					index= table.getSelectedRow();
					new YZXXGLBJ().setVisible(true);
					dispose();
				}else {
					JOptionPane.showMessageDialog(null, "请先选择要修改的用户");
				}
			}
		});
		button_3.setFont(new Font("宋体", Font.PLAIN, 24));
		button_3.setBounds(15, 462, 203, 70);
		contentPane.add(button_3);
		
		JButton button_4 = new JButton("删除信息");
		button_4.setIcon(new ImageIcon(YZXXGL.class.getResource("/com/ejia/photo/删除.png")));
		button_4.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (table.getSelectedRowCount()>0) {
					int index=table.getSelectedRow();
					int id=users.get(index).getId();
					if (UserService.delete(id) && UserService.deletehouse(users.get(index).getHid().getHid()) && UserService.deletecar(id)) {
						UserService.deletemoney(id);
						JOptionPane.showMessageDialog(null, "删除成功");
						dataBind("");
					}else {
						JOptionPane.showMessageDialog(null, "删除失败");
					}
			
				}else {
					JOptionPane.showMessageDialog(null, "请选择用户");
				}
		}
			});
		button_4.setFont(new Font("宋体", Font.PLAIN, 24));
		button_4.setBounds(15, 548, 203, 70);
		contentPane.add(button_4);
		
		JLabel label = new JLabel("请输入业主姓名");
		label.setHorizontalAlignment(SwingConstants.CENTER);
		label.setFont(new Font("宋体", Font.PLAIN, 42));
		label.setBounds(106, 15, 318, 68);
		contentPane.add(label);
		
		txtName = new JTextField();
		txtName.setFont(new Font("宋体", Font.PLAIN, 24));
		txtName.setBounds(455, 36, 214, 39);
		contentPane.add(txtName);
		txtName.setColumns(10);
		
		JButton button_6 = new JButton("查询");
		button_6.setIcon(new ImageIcon(YZXXGL.class.getResource("/com/ejia/photo/查询.png")));
		button_6.setFont(new Font("宋体", Font.PLAIN, 42));
		button_6.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				dataBind(txtName.getText());
			}
		});
		button_6.setBounds(724, 19, 199, 60);
		contentPane.add(button_6);
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(233, 114, 784, 504);
		contentPane.add(scrollPane);
		
		table = new JTable();
		dataBind("");
		scrollPane.setViewportView(table);
		
		JButton button_7 = new JButton("返回");
		button_7.setIcon(new ImageIcon(YZXXGL.class.getResource("/com/ejia/photo/返回 (1).png")));
		button_7.setFont(new Font("宋体", Font.PLAIN, 24));
		button_7.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				new Main().setVisible(true);
				dispose();
			}
		});
		button_7.setBounds(15, 119, 203, 70);
		contentPane.add(button_7);
		setLocationRelativeTo(null);
	}

}
